/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo.Controler;

import Model.Beans.Book;
import Model.Servicios.BookSrv;
import com.opensymphony.xwork2.ActionSupport;

/**
 *
 * @author Eduardo
 */
public class InsBooksEditAction extends ActionSupport {
    private BookSrv bSrv = new BookSrv();
    private String id;
    private Book book;
    
    public String update() {
        setBook(bSrv.getBook(Integer.valueOf(id)));
        return "update";
    }
    
    public String complete() {
        Book bookEdited = bSrv.getBook(book.getIdbook());
        bookEdited.setName(book.getName());
        bookEdited.setAuthor(book.getAuthor());
        bookEdited.setIsbn(book.getIsbn());
        bookEdited.setEditorial(book.getEditorial());
        bookEdited.setYear(book.getYear());
        bSrv.update(bookEdited);
        addActionMessage("Los datos han sido actualizados!");
        return "complete";
    }
    
    @Override
    public void validate() {
        if (getBook() != null) {
            if (getBook().getName().equals("")) {
                addFieldError("book.name", "Falta nombre!");
            }
            if (getBook().getAuthor().equals("")) {
                addFieldError("book.author", "Falta Autor!");
            }
            if (getBook().getEditorial().equals("")) {
                addFieldError("book.editorial", "Falta editorial!");
            }
            if (getBook().getYear() == null) {
                addFieldError("book.year", "Falta anio!");
            }
        }
    }
    
    public String delete() {
        Book bookToDelete = bSrv.getBook(Integer.valueOf(id));
        bSrv.delete(bookToDelete);
        return "delete";
    }
    
    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
